package org.ourgrid.broker.business.schedulerdata.executor;

import java.util.ArrayList;
import java.util.List;

import org.ourgrid.broker.business.dao.BrokerDAOFactory;
import org.ourgrid.broker.business.schedulerdata.SchedulerData;
import org.ourgrid.common.internal.IResponseTO;
import org.ourgrid.common.internal.response.OperationSucceedResponseTO;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class OperationSucceedExecutor extends AbstractSchedulerDataExecutor {
	
	/**
	 * <OPERATION_SUCCEED errorMessage='String' serializable='String'/>
	 */
	public List<IResponseTO> execute(SchedulerData command) {
		List<IResponseTO> responses = new ArrayList<IResponseTO>();
		
		Document logDocFile = super.getXMl(command.getXml());
		
		Element element = logDocFile.getDocumentElement();
		
		String errorMessage = element.getAttribute("errorMessage");
		String serializable = element.getAttribute("serializable");

		OperationSucceedResponseTO to = new OperationSucceedResponseTO();

		String clientAddress = BrokerDAOFactory.getInstance().getJobDAO().getBrokerControlClientAddress();
		
		to.setClientAddress(clientAddress);
		if (errorMessage != null && !errorMessage.equals("")) {
			to.setErrorCause(new Exception(errorMessage));
		} else if (serializable != null) {
			to.setResult(serializable);
		}
		
		responses.add(to);
		
		return responses;
	}
}
